package com.szt.sy.controller.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.szt.sy.controller.base.AdminBaseController;
import com.szt.sy.entity.SessionUser;
import com.szt.sy.entity.system.User;
import com.szt.sy.service.system.button.ButtonService;
import com.szt.sy.service.system.menu.MenuService;
import com.szt.sy.service.system.role.RoleService;
import com.szt.sy.service.system.rolefhbutton.RoleFhbuttonService;
import com.szt.sy.service.system.user.UserService;

/***
 * 后台登录-控制器
 * 
 * @author wwy
 *
 */
@Controller
@RequestMapping("admin")
public class AdminMainController extends AdminBaseController {
	
	private static final Logger log = LoggerFactory.getLogger(AdminMainController.class);
	
	@Autowired
	private UserService userService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private RoleFhbuttonService buttonrightsService;
	@Autowired
	private ButtonService buttonService;
	
	/**
	 * 访问登录页
	 *
	 * @return
	 * @throws Exception
	 */
	@GetMapping(value = "/login")
	public String toLogin(HttpServletRequest request, HttpSession httpSession) throws Exception {
		httpSession.invalidate();
		httpSession = request.getSession(true);
		return "system/index/login";
	}
	
	/**
	 * 访问系统首页
	 *
	 * @param changeMenu：切换菜单参数
	 * @return
	 */
	@RequestMapping(value = "/main")
	public ModelAndView loginIndex(@RequestParam Map<String, Object> map, HttpSession httpSession) {
		ModelAndView mv = this.getModelAndView();
		// 读取session中的用户信息
		SessionUser sessionUser = getLoginSessionUser();
		User user = sessionUser.getUser();
		mv.setViewName("system/index/main");
		mv.addObject("user", user);
		mv.addObject("menuList", sessionUser.getMenuList());
		mv.addObject("pd", map);
		return mv;
	}
	
	/**
	 * 进入tab标签
	 *
	 * @return
	 */
	@RequestMapping(value = "/tab")
	public String tab() {
		return "system/index/tab";
	}
	
	/**
	 * 进入首页后的默认页面
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/login_default")
	public ModelAndView defaultPage() throws Exception {
		ModelAndView mv = this.getModelAndView();
		SessionUser sessionUser = getLoginSessionUser();
		User user = getLoginUser();
		String viewName = null;
		if (StringUtils.isNotBlank(user.getRole().getIndexUrl())) {
			viewName = user.getRole().getIndexUrl();
		}
		if (StringUtils.isNotBlank(viewName)) {
			mv.setViewName("redirect:" + viewName);
			return mv;
		} else {
			viewName = "system/index/null";
		}
		mv.setViewName(viewName);
		return mv;
	}
	
	@RequestMapping(value = "/login_admin")
	public ModelAndView adminDefaultPage() throws Exception {
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("system/index/default");
		return mv;
	}
	
	/**
	 * 用户注销
	 *
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/logout")
	public ModelAndView logout(HttpSession httpSession) {
		ModelAndView mv = this.getModelAndView();
		// 以下清除session缓存
		httpSession.invalidate();
		mv.setViewName("redirect:/admin/login");
		return mv;
	}
	
}
